package jrain.fw.core.http;

import java.util.HashMap;
import java.util.Map;

import jrain.utils.consts.BaseConst;

public class HttpRequest {

	public static final String CONTENT_TYPE = "Content-Type";

	private String url;

	private String encoding = BaseConst.UTF8.name();

	private HttpMethod method = HttpMethod.POST;

	private HttpContentFormatType httpContentType = null;

	private int connectTimeout = -1;

	private int readTimeout = -1;

	private Map<String, String> header = new HashMap<String, String>();

	private Map<String, Object> data = new HashMap<String, Object>();

	private byte[] content;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getEncoding() {
		return encoding;
	}

	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}

	public HttpMethod getMethod() {
		return method;
	}

	public void setMethod(HttpMethod method) {
		this.method = method;
	}

	public int getConnectTimeout() {
		return connectTimeout;
	}

	public void setConnectTimeout(int connectTimeout) {
		this.connectTimeout = connectTimeout;
	}

	public int getReadTimeout() {
		return readTimeout;
	}

	public void setReadTimeout(int readTimeout) {
		this.readTimeout = readTimeout;
	}

	public Map<String, String> getHeader() {
		return header;
	}

	public void setHeader(Map<String, String> header) {
		this.header = header;
	}

	public String getHeader(String key) {
		return this.header.get(key);
	}

	public void putHeader(String key, String value) {
		this.header.put(key, value);
	}

	public void removeHeader(String key) {
		this.header.remove(key);
	}

	public void clearHeader(String key) {
		this.header.clear();
	}

	public HttpContentFormatType getHttpContentType() {
		return httpContentType;
	}

	public void setHttpContentType(HttpContentFormatType httpContentType) {
		this.httpContentType = httpContentType;
		if (this.httpContentType != null) {
			putHeader(CONTENT_TYPE, getContentTypeValue(this.httpContentType, this.encoding));
		}
	}

	public Map<String, Object> getData() {
		return data;
	}

	public void setData(Map<String, Object> data) {
		this.data = data;
	}

	public Object getData(String key) {
		return this.data.get(key);
	}

	public void putData(String key, Object value) {
		this.data.put(key, value);
	}

	public void removeData(String key) {
		this.data.remove(key);
	}

	public void clearData(String key) {
		this.data.clear();
	}

	public byte[] getContent() {
		return content;
	}

	public void setContent(byte[] content) {
		this.content = content;
	}

	
	public String getContentTypeValue(HttpContentFormatType contentType, String encoding) {
		if (null != contentType && null != encoding) {
			return HttpContentFormatType.mapFormatToAccept(contentType) + ";charset=" + encoding.toLowerCase();
		} else if (null != contentType) {
			return HttpContentFormatType.mapFormatToAccept(contentType);
		}
		return null;
	}

}
